import { _decorator, Component, Node, AudioClip, AudioSource } from 'cc'
const { ccclass, property } = _decorator

@ccclass('sound')
export class sound extends Component {
  @property([AudioClip])
  soundList: AudioClip[] = []

  private soundData = {}

  private audioSource: AudioSource = null

  start() {
    this.audioSource = this.node.getComponent(AudioSource)
    for (let index = 0; index < this.soundList.length; index++) {
      const element = this.soundList[index]
      this.soundData[element.name] = element
    }
  }

  soundPlay(name: string) {
    const sound = this.soundData[name]
    if (sound) {
      this.audioSource.playOneShot(sound)
    }
  }
}
